package com.company.service.impl;

import com.company.dao.CheckItemDao;
import com.company.entity.PageResult;
import com.company.pojo.CheckItem;
import com.company.service.CheckItemService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author
 * @date 2021-08-03 18:55
 */

@DubboService(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;


    @Override
    public int add(CheckItem checkItem) {
        int add = checkItemDao.add(checkItem);
        return add;
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void deleteCheckItem(Integer id) {
        /**
         * 由于t_checkItem表与t_checkGroup表之间是多对多的关系，所以不能之间删除里面的记录
         * 必须先删除关联表中的数据，然后再删除CheckItem中的记录
         */
        Integer count = checkItemDao.findCountById(id);
        if(count>0){
            /*先删除关联表中的记录*/
            checkItemDao.deleteCheckGroup_CheckItem(id);
        }
        checkItemDao.deleteCheckItem(id);
    }

    @Override
    public void editCheckItem(CheckItem checkItem) {
        checkItemDao.editCheckItem(checkItem);
    }

    @Override
    public CheckItem findByIdCheckItem(Integer id) {
        return (CheckItem)checkItemDao.findByIdCheckItem(id);
    }


}
